#include "Cube.h"
#include <cassert>
#include <math3d.h>
#include "random.h"

const float_t RENDER_2D_FACTOR = 1.0f;

Cube::Cube(void)
{
	m_physicsObject = new ImpossiblePhysics::PhysicsObj();
	m_physicsObject->idString = ImpossiblePhysics::ENV_OBJECT;

	//give the cude a random color 
	topColor = getRangedRandom(0.0f, 1.0f);
}


Cube::~Cube(void)
{
	// These should always exist before destruction
	assert(m_physicsObject && m_physicsObject->body);

	delete m_physicsObject->body->getMotionState();
	delete m_physicsObject->body->getCollisionShape();

	delete m_physicsObject->body;
	delete m_physicsObject;
}

void Cube::Update(float_t argDelta)
{
}

void Cube::Draw() const
{
	glPushMatrix();
	// Transform this object in the scene

	btVector3 _halfWidths = ((btBoxShape*)m_physicsObject->body->getCollisionShape())->getHalfExtentsWithMargin();

	btTransform _transform;
	float_t _mat[16];

	m_physicsObject->body->getMotionState()->getWorldTransform(_transform);
	_transform.getOpenGLMatrix(_mat);
	glMultMatrixf(_mat);

	glBegin(GL_QUADS);                  // Start Drawing The Cube

	// Top face
	//apply a random color 
	//glColor3f(0.0f,topColor,0.0f);
	glNormal3f(0.0f, 1.0f, 0.0f);
	glVertex3f( _halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
	glVertex3f( _halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
											 				  
	// Bottom face							 				  
	//glColor3f(0.0f,1.0f,0.0f);  
	glNormal3f(0.0f, -1.0f, 0.0f);
	glVertex3f( _halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());
	glVertex3f( _halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());      
											 				  
	// Front face							 				  
	//glColor3f(_halfWidths.x(),0.0f,0.0f);    
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f( _halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
	glVertex3f(-_halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
	glVertex3f( _halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
											 				  
	// Back face							 				  
	//glColor3f(1.0f,0.0f,0.0f);  
	glNormal3f(0.0f, 0.0f, -1.0f);
	glVertex3f( _halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
	glVertex3f( _halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
											 				  
	// Left face							 				  
	//glColor3f(0.0f,0.0f,1.0f); 
	glNormal3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(-_halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
	glVertex3f(-_halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());      
	glVertex3f(-_halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
											 				  
	// Right face							 				  
	//glColor3f(1.0f,0.0f,1.0f);    
	glNormal3f(1.0f, 0.0f, 0.0f);
	glVertex3f( _halfWidths.x(), _halfWidths.y(),-_halfWidths.z());      
	glVertex3f( _halfWidths.x(), _halfWidths.y(), _halfWidths.z());      
	glVertex3f( _halfWidths.x(),-_halfWidths.y(), _halfWidths.z());      
	glVertex3f( _halfWidths.x(),-_halfWidths.y(),-_halfWidths.z());

	
	glEnd();
	glPopMatrix();
}

void Cube::Draw2D() const
{
	btTransform t;
	m_physicsObject->body->getMotionState()->getWorldTransform(t);
	btVector3 o = t.getOrigin();

	btVector3 _halfWidths = ((btBoxShape*)m_physicsObject->body->getCollisionShape())->getHalfExtentsWithMargin();

	float_t halfWidthX = _halfWidths.z() * RENDER_2D_FACTOR;
	float_t halfWidthY = _halfWidths.y() * RENDER_2D_FACTOR;

	float_t x = (o.z() * -RENDER_2D_FACTOR) - halfWidthX;
	float_t y = (o.y() * -RENDER_2D_FACTOR) - halfWidthY;

	// draw

	glLoadIdentity();

	glColor3f(1.0f,0.0f,0.0f);  

	glBegin(GL_QUADS);

	glVertex3f(x + halfWidthX * 2, y, 0);
	glVertex3f(x, y, 0);
	glVertex3f(x, y + halfWidthY * 2, 0);
	glVertex3f(x + halfWidthX * 2, y + halfWidthY * 2, 0);


	glEnd();
}
